import os
import sys
import datetime
import django

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()
from apps.supply_order.models import SupplyOrder
from apps.supply_order.models import OrderIncome
from wechatpy.oauth import WeChatOAuth
from wechatpy.component import ComponentOAuth
from core.tools.encryption import md5
from core.wchat.WXInstance import KLBWxBase as wx_base


# 分佣
# def test():
#     s_order = SupplyOrder.objects.get(id=249)
#     parent_order = s_order.main_base_order.all()
#     for parent in parent_order:
#         for child in parent.child_order.all():
#             base_income = OrderIncome.objects.create_referee_income(child)
#             if base_income:
#                 OrderIncome.objects.create_other_income(base_income)

# 微信开放平台用户登录
# def test():
#     app_id = "wxc7eadf63b02d39bd"
#     secret = "6990f2aa3e54f42a18c56ea1c7a8a14c"
#     # redirect_uri = "https://shop.kalaibao.com/h5/index.html"
#     redirect_uri = "http://shop.kalaibao.com"
#     weChatOAuth = WeChatOAuth(app_id, secret, redirect_uri, scope='snsapi_login')
#     url = weChatOAuth.authorize_url
#     print(url)

# 微信公众平台
def test():
    redirect_uri = "https://shop.kalaibao.com/h5/index.html#/"
    weChatOAuth = wx_base.get_we_chat_OAuth(redirect_uri, scope='snsapi_userinfo')
    url = weChatOAuth.authorize_url
    print(url)
    code = "001gzJbN1T98F716zd8N1V4HbN1gzJb-"
    res = weChatOAuth.fetch_access_token(code)
    print(res)
    res = {
        'access_token': '20_DMPXFmGKY61fKJtxuSoZvwJl5wMFcpGzVJF1GoXI2YiaTarkWdIxp4yDhk2Mt6fFE4MSvwhaorqRnBj-mcomhk8W14a2Zj_x5Jx5hWaIB7o',
        'expires_in': 7200,
        'refresh_token': '20_tC8sdCftck4AjEphoW34yUx3UtE8I_FvnVH4aqFoIR5lqs57cNb8_k4DI1UlDC4KjjNGC5C1RXmnectnyxQaRulz-PYz-UjQt2y9MUm5Vmg',
        'openid': 'o-OFyw70A9yEKSJ0Jk-BB1BYgCac', 'scope': 'snsapi_userinfo', 'unionid': 'oLKNFsyLm4SY3LwuYV9g17_r14SA'}
    access_token = res.get("access_token")
    openid = res.get("openid")
    res = weChatOAuth.get_user_info(openid, access_token)
    print(res)


def test1():
    a = md5(md5("123"))
    print(len(a))


def test_url():
    redirect_uri = 'https://shop.kalaibao.com/h5/index.html'
    weChatOAuth = wx_base.get_we_chat_OAuth(redirect_uri, scope='snsapi_userinfo')
    url = weChatOAuth.authorize_url
    print(url)


# def test_code():
#     res = weChatOAuth.fetch_access_token(code)
#     access_token = res.get("access_token")
#     openid = res.get("openid")
#     res = weChatOAuth.get_user_info(openid, access_token)


"""
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxd229df499f16b723&redirect_uri=https%3A%2F%2Fshop.kalaibao.com&response_type=code&scope=snsapi_userinfo#wechat_redirect
{'access_token': '20_DMPXFmGKY61fKJtxuSoZvwJl5wMFcpGzVJF1GoXI2YiaTarkWdIxp4yDhk2Mt6fFE4MSvwhaorqRnBj-mcomhk8W14a2Zj_x5Jx5hWaIB7o', 'expires_in': 7200, 'refresh_token': '20_tC8sdCftck4AjEphoW34yUx3UtE8I_FvnVH4aqFoIR5lqs57cNb8_k4DI1UlDC4KjjNGC5C1RXmnectnyxQaRulz-PYz-UjQt2y9MUm5Vmg', 'openid': 'o-OFyw70A9yEKSJ0Jk-BB1BYgCac', 'scope': 'snsapi_userinfo', 'unionid': 'oLKNFsyLm4SY3LwuYV9g17_r14SA'}
{'openid': 'o-OFyw70A9yEKSJ0Jk-BB1BYgCac', 'nickname': 'A/远远远 远走高飞:(', 'sex': 1, 'language': 'zh_CN', 'city': '鸡西', 'province': '黑龙江', 'country': '中国', 'headimgurl': 'http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erAicnu11ExsS5u7UZGpguJFOFO0yicOEFqJXL61Vst6P5vKdh6z8Z8D9alKK0ZMzVv3kFMicPg5lXqQ/132', 'privilege': [], 'unionid': 'oLKNFsyLm4SY3LwuYV9g17_r14SA'}
"""
if __name__ == '__main__':
    test_url()
    # OrderIncome.objects.get(income_type=1)
